package com.atguigu.gulimail.product.service.impl;

import com.atguigu.gulimail.product.entity.BrandEntity;
import com.atguigu.gulimail.product.entity.CategoryEntity;
import com.atguigu.gulimail.product.service.BrandService;
import com.atguigu.gulimail.product.service.CategoryService;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.atguigu.common.utils.PageUtils;
import com.atguigu.common.utils.Query;

import com.atguigu.gulimail.product.dao.CategoryBrandRelationDao;
import com.atguigu.gulimail.product.entity.CategoryBrandRelationEntity;
import com.atguigu.gulimail.product.service.CategoryBrandRelationService;


@Service("categoryBrandRelationService")
public class CategoryBrandRelationServiceImpl extends ServiceImpl<CategoryBrandRelationDao, CategoryBrandRelationEntity> implements CategoryBrandRelationService {

    @Autowired
    CategoryService categoryService;

    @Autowired
    BrandService brandService;

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<CategoryBrandRelationEntity> page = this.page(
                new Query<CategoryBrandRelationEntity>().getPage(params),
                new QueryWrapper<CategoryBrandRelationEntity>()
        );

        return new PageUtils(page);
    }

    @Override
    public void saveAll(CategoryBrandRelationEntity categoryBrandRelation) {
        CategoryEntity category = categoryService.getById(categoryBrandRelation.getCatelogId());
        BrandEntity brand = brandService.getById(categoryBrandRelation.getBrandId());

        categoryBrandRelation.setBrandName(brand.getName());
        categoryBrandRelation.setCatelogName(category.getName());

        this.save(categoryBrandRelation);
    }

    @Override
    public void updateBrand(Long brandId, String name) {
        CategoryBrandRelationEntity categoryBrandRelationEntity = new CategoryBrandRelationEntity();
        categoryBrandRelationEntity.setBrandId(brandId);
        categoryBrandRelationEntity.setBrandName(name);

        LambdaUpdateWrapper<CategoryBrandRelationEntity> updateWrapper=new LambdaUpdateWrapper<>();
        updateWrapper.eq(CategoryBrandRelationEntity::getBrandId,brandId);
        this.update(categoryBrandRelationEntity,updateWrapper);
    }

    @Override
    public void updateCategory(Long Id,String Name) {

        /**
         * 方法一
         */
//        CategoryBrandRelationEntity categoryBrandRelationEntity=new CategoryBrandRelationEntity();
//        categoryBrandRelationEntity.setCatelogName(Name);
//        categoryBrandRelationEntity.setCatelogId(Id);
//
//        LambdaUpdateWrapper<CategoryBrandRelationEntity> updateWrapper=new LambdaUpdateWrapper<>();
//        updateWrapper.eq(CategoryBrandRelationEntity::getCatelogId,Id);
//
//        this.update(categoryBrandRelationEntity,updateWrapper);

        /**
         * 方法二
         */
        this.baseMapper.updateCategory(Id,Name);
    }

    @Override
    public List<BrandEntity> getBrandsByCatId(Long catId) {
        List<CategoryBrandRelationEntity> relationEntityList=new ArrayList<>();

        if(catId==0){
            throw new RuntimeException("该分类没有相关品牌");
        }else {
            relationEntityList = this.list(new QueryWrapper<CategoryBrandRelationEntity>().eq("catelog_id", catId));
        }

        List<Long> brandIds=relationEntityList.stream().map((i)->{
            return i.getBrandId();
        }).collect(Collectors.toList());
        List<BrandEntity> brandEntities = brandService.list(new QueryWrapper<BrandEntity>().in("brand_id", brandIds));

        return brandEntities;
    }

}